﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace VideoNetApplication.Common.Helper
{
    public class EmailHelper
    {
        #region 邮件属性
        private string userName = ConfigurationManager.AppSettings["EmailUser"].ToString();   //电子邮箱用户名

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string password = ConfigurationManager.AppSettings["EmailPwd"].ToString();        //电子邮箱密码

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        private string from = ConfigurationManager.AppSettings["MailFrom"].ToString();            //发件者邮箱

        public string From
        {
            get { return from; }
            set { from = value; }
        }

        private string smtpServerName = ConfigurationManager.AppSettings["SMTPServerName"].ToString();
        /// <summary>
        /// SMTP服务器名称
        /// </summary>
        public string SMTPServerName
        {
            set { smtpServerName = value; }
            get { return smtpServerName; }
        }
        #endregion


        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="mailTo">收件人</param>
        /// <param name="subject">标题</param>
        /// <param name="body">内容</param>
        /// <returns></returns>
        public bool Send(string mailTo, string subject, string body)
        {
            try
            {
                MailAddress from = new MailAddress(this.From);
                MailAddress to = new MailAddress(mailTo);
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

                message.Subject = subject;
                message.Body = body;
                SmtpClient client = new SmtpClient(smtpServerName);
                client.Credentials = new NetworkCredential(userName, password);
                client.Send(message);
                return true;
            }
            catch (System.Net.Mail.SmtpException e)
            {
                //LogHelper.Error("邮件发送失败：\n 用户名：" + userName + "\n 密码：" + password + "\n 邮件内容：" + body + "\n 错误信息:" + e.StackTrace);
                return false;
            }


        }

        //public bool SendManyMail(List<EmailDetail> emails, EmailCode ecode)
        //{
        //    if (emails.Count > 0)
        //    {
        //        foreach (var e in emails)
        //        {
        //            Send(e.Email, e.Title, e.Content, ecode);
        //        }
        //        return true;
        //    }
        //    else
        //    {

        //        return false;
        //    }
        //}



    }
}
